<template>
  <el-dialog title="查询所需数据"
             :visible.sync="visible"
             :close-on-click-modal="false"
             :close-on-press-escape="false"
             :append-to-body="true"
             :before-close="close">
    <el-row :gutter="20">
      <el-form>
        <el-col :span="8"
                v-for="item in fileds"
                :key="item.paramName">
          <el-form-item :label="item.paramDesc || item.paramName">
            <el-input-number v-if="item.paramType === 'java.lang.Integer' || item.paramType === 'java.lang.Long' || item.paramType === 'java.lang.Byte' || item.paramType === 'java.math.BigDecimal' || item.paramType === 'java.lang.Double'||item.paramType === 'java.lang.Float'"
                             v-model="form[item.paramName]"
                             @blur="changeNumber(item.paramType,item.paramName)"
                             :controls="false"></el-input-number>
            <el-input v-else-if="item.paramType === 'java.lang.String'"
                      v-model="form[item.paramName]"></el-input>
            <el-date-picker v-else-if="item.paramType === 'java.util.Date'"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            v-model="form[item.paramName]"
                            type="datetime">
            </el-date-picker>
            <el-select v-else-if="item.paramType === 'java.lang.Boolean'"
                       v-model="form[item.paramName]">
              <el-option v-for="option in [true,false]"
                         :key="option"
                         :label="option">{{option}}</el-option>
            </el-select>
            <el-input v-model="form[item.paramName]"
                      v-else-if="item.paramType === 'java.lang.String[]' || item.paramType === 'java.lang.Integer[]'|| item.paramType === 'java.lang.Long[]'|| item.paramType === 'java.lang.Double[]'|| item.paramType === 'java.lang.Float[]'|| item.paramType === 'java.lang.Byte[]'"></el-input>
            <span v-else></span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="　">
            <el-button size="mini"
                       @click="emptyObj">清 空</el-button>
            <el-button type="primary"
                       size="mini"
                       @click="search">查 询</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <avue-crud :option="option"
               :data="tableData"
               :table-loading="loading"
               @current-row-change="handleCurrentRowChange"></avue-crud>
    <span slot="footer"
          class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary"
                 @click="confirm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getTree, getUrlParams } from '../../api/admin/business'
import request from '../../router/axios'
import { option } from './config'

export default {
  props: {
    searchObj: Object
  },
  watch: {
    searchObj: {
      deep: true,
      immediate: true,
      handler: 'getInfo'
    }
  },
  data () {
    return {
      visible: false,
      fileds: [],
      form: {},
      parseObj: {},
      option: option(this),
      tableData: [],
      loading: false,
      currentRow: {}
    }
  },
  methods: {
    getInfo () {
      getTree().then(res => {
        this.parseObj = this.parseData(res.data.data, this.searchObj.referBusiFunc)
        console.log(this.parseObj)
        try {
          this.fileds = JSON.parse(this.parseObj.params)
          this.option.column = JSON.parse(this.parseObj.listFields).list.map(res => { return { label: res.returnDesc, prop: res.returnName } })
        } catch (error) {
          this.fileds = []
          this.option.column = []
        }
      })
    },
    parseData (data, target, result = '') {
      if (Array.isArray(data))
        data.some(res => {
          if (res.children.length > 0) result = this.parseData(res.children, target, result)
          if (res.id === target) {
            result = res
            return true
          }
        })
      return result
    },
    changeNumber (type, name) {
      if (!this.form[name]) return
      if (type !== 'java.lang.Double' && type !== 'java.lang.Float') this.form[name] = this.form[name].toFixed(0)
    },
    close () {
      this.visible = false
    },
    search () {
      const obj = Object.assign({}, this.form)
      this.loading = true
      this.fileds.some(res => {
        if (res.paramType.indexOf('[]') > -1) obj[res.paramName] = obj[res.paramName].split(',')
      })
      request({
        url: this.parseObj.routePrefix + this.parseObj.baseUrl.split(',')[1],
        method: 'get',
        params: obj
      }).then(res => {
        this.$message({
          type: res.data.meta.success ? 'success' : 'error',
          message: res.data.meta.message
        })
        this.loading = false
        this.tableData = res.data.data.records || res.data.data
      })
    },
    emptyObj () {
      this.form = {}
    },
    handleCurrentRowChange (val) {
      this.currentRow = val
    },
    confirm () {
      if (!this.currentRow) {
        this.$message.warning('请选择一条数据')
        return
      }
      this.$emit('confirm', { data: this.currentRow, type: JSON.parse(this.parseObj.listFields), searchObj: this.searchObj })
      this.visible = false
    }
  }
}
</script>

<style lang="scss" scoped>
/deep/.avue-crud {
  width: 100%;
}
/deep/.avue-crud__menu {
  display: none;
}
</style>